layui.config({
    base : "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index','form','jquery', 'table'],function(){
    var form = layui.form,
        table=layui.table,
        admin  = layui.admin,
        $ = layui.jquery;

    var addUserIndex = null;

    table.render({
        elem: '#enterprise_profit_table'
        ,url: '/enterprise_profit/queryData'
        ,method:"post"
        ,height: 500
        ,limit:50							//显示数目
        ,limits:[50,100]
        ,cols: [[
            {field:'id', title: 'id',width:100, "align":"center"}
            ,{field:'name',width:200, title: '分润名称',"align":"center"}
            ,{field:'consumeProfit',width:200, title: '分润返还(百分比)',"align":"center",templet:function (d){
                    return d.consumeProfit==null?0:new Number(d.consumeProfit.toString()).mul(new Number(100));
                }}
           /* ,{field:'recConsumeProfit',width:200, title: '官网补贴(百分比)',"align":"center",templet:function (d){
                    return d.recConsumeProfit==null?0:new Number(d.recConsumeProfit.toString()).mul(new Number(100));
                }}*/
            ,{field:'createdTime',width:200, title: '创建时间',"align":"center"}

            ,{field:'OPT', width:100, title: '操作',"align":"center",templet:function (d) {
                    return     '<a  href="#" class="layui-btn layui-btn-xs" lay-event="modify_profit" >编辑</a>';
                }}
        ]]
        ,request:{
            pageName: 'pageNo' //页码的参数名称，默认：page
            ,limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where:{}
        ,id: 'enterprise_profit_table'
        ,page: true
    });

    table.on('tool(enterprise_profit_table)', function(obj){

        admin.popup({
            title: '编辑'
            ,shade: 0
            ,anim: -1
            ,offset:"20px"
            ,area: ['80%', '500px']
            ,skin: 'layui-anim layui-anim-upbit'
            ,content:$("#add_profit_win")
            ,end:function () {
                table.reload('enterprise_profit_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {
                    }
                });
            }
        });

        var table_event = obj.event;

        var data = obj.data;

        if(table_event=="modify_profit"){
            form.val('add_profit_form', {
                "id":data.id,
                "name":data.name,
                "recConsumeProfit": data.recConsumeProfit==null?0:new Number(data.recConsumeProfit.toString()).mul(new Number(100)),
                "consumeProfit": data.consumeProfit==null?0:new Number(data.consumeProfit.toString()).mul(new Number(100))
            })

        }

    });

    //添加等级
    $(".profitAdd_btn").click(function(){

        $("#add_profit_form")[0].reset();
        admin.popup({
            title: '编辑'
            ,shade: 0
            ,anim: -1
            ,offset:"20px"
            ,area: ['80%', '500px']
            ,skin: 'layui-anim layui-anim-upbit'
            ,content:$("#add_profit_win")
            ,end:function () {
                table.reload('enterprise_profit_table', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    ,where: {

                    }
                });
            }
        });
    });




    /**
     * oem 新增提交
     */
    form.on('submit(addprofitOk)',function (data) {
        data.field.recConsumeProfit =data.field.recConsumeProfit/100;
        data.field.consumeProfit =data.field.consumeProfit/100;

        $.ajax({
            url:"/enterprise_profit/edit",
            data:data.field,
            type:"post",
            success:function (result) {
                if (result.code == 1){
                    layer.msg("编辑成功",{
                        offset: '32%'
                        ,icon: 1
                        ,time: 0
                        ,btn: '知道了'
                        ,btnAlign:"c"
                        ,shade:0.4
                        ,yes:function () {
                            table.reload('enterprise_profit_table', {
                            });
                            layer.closeAll();
                        }
                    })
                }else{
                    layer.msg(result.message,{
                        offset: '32%'
                        ,icon: 2
                        ,time: 3000
                    },function () {
                        layer.closeAll();
                    })
                }

            }
        });
        return false;
    });
})

